package com.abcdefg.rabbitmq.controller;

import com.abcdefg.rabbitmq.constant.Constants;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("producer")
@RestController
public class ProducerController {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("work")
    public String work() {
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend("", Constants.WORK_QUEUE,
                    "work_queue..."+i);
        }
        return "work:发送成功!";
    }
    @RequestMapping("fanout")
    public String fanout() {
        rabbitTemplate.convertAndSend(Constants.FANOUT_EXCHANGE, "",
                "fanout_queue...");
        return "fanout:发送成功!";
    }

    @RequestMapping("direct/{routingKey}")
    public String fanout(@PathVariable("routingKey") String routingKey) {
        rabbitTemplate.convertAndSend(Constants.DIRECT_EXCHANGE, routingKey,
                "direct_queue..."+routingKey);
        return "direct:发送成功!";
    }

    @RequestMapping("topic/{routingKey}")
    public String topic(@PathVariable("routingKey") String routingKey) {
        rabbitTemplate.convertAndSend(Constants.TOPIC_EXCHANGE, routingKey,
                "topic_queue..."+routingKey);
        return "topic:发送成功!";
    }
}
